const app = getApp()
import { smsSend, qcodeBind, getDecryptInfo } from '../../utils/networkRequest.js'
import { errorMsg } from '../../utils/tool.js'
const Dialog = require('../../zanui/dialog/dialog');
Page({
  /**
   * 页面的初始数据
   * keyboard1:首页键盘,显示省的简称
   * keyboard2:第二页键盘，显示数字和大写字母
   */
  
  data: {
    isKeyboard: false,//是否显示键盘
    specialBtn: false,
    tapNum: false,//数字键盘是否可以点击
    parkingData: false,//是否展示剩余车位按钮
    isFocus: false,//输入框聚焦
    isFocusP: false,//手机输入框聚焦
    isFocusV: false,//验证码输入框聚焦
    isFocusN: false,//姓名输入框聚焦
    flag: false,//防止多次点击的阀门 
    keyboardNumber: '1234567890',
    keyboardAlph: 'QWERTYU OPASDFGHJKLZXCVBNM巛',
    keyboard1: '京津沪冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤川青藏琼宁渝',
    keyboard2: '',
    keyboard2For: ['完成'],
    keyboardValue: '',
    textArr: [],
    textValue: '', //车牌号
    inputPhoneValue: "",//手机号
    inputNameValue: "",//姓名
    verCode: "", //验证码
    downtime: 60,
    qcode:"", //二維碼編號 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qcode:options.qcode,
    })
  },
  /**
   * 输入框显示键盘状态
   */
  showKeyboard: function () {
    var self = this;
    self.setData({
      isFocus: true,
      isKeyboard: true,
    })
  },
  /**
 * 手机输入框显示键盘状态
 */
  showPhoneKeyboard: function () {
    this.hideKeyboard();
    var self = this;
    self.setData({
      isFocusP: true,
    })
  },
  /**
 * 手机输入框隐藏键盘状态
 */
  hidePhoneKeyboard: function () {

    var self = this;
    self.setData({
      isFocusP: false,
    })
  },
  /**
   * 验证码输入框显示键盘状态
   */
  showCodeKeyboard: function () {
    this.hideKeyboard();
    var self = this;
    self.setData({
      isFocusV: true,
    })
  },
  /**
 * 验证码输入框隐藏键盘状态
 */
  hideCodeKeyboard: function () {

    var self = this;
    self.setData({
      isFocusV: false,
    })
  },
  /**
 * 姓名输入框显示键盘状态
 */
  showNameKeyboard: function () {
    this.hideKeyboard();
    var self = this;
    self.setData({
      isFocusN: true,
    })
  },
  /**
 * 姓名输入框隐藏键盘状态
 */
  hideNameKeyboard: function () {
    var self = this;
    self.setData({
      isFocusN: false,
    })
  },

  /**
    * 手机号接收
    */
  bindPhoneKeyInput: function (e) {
    this.setData({
      inputPhoneValue: e.detail.value
    })
  },
  /**
   * 姓名接收
   */
  bindNameKeyInput: function (e) {
    this.setData({
      inputNameValue: e.detail.value
    })
  },
  /**
   * 验证码接收
   */
  bindCodeKeyInput: function (e) {
    this.setData({
      verCode: e.detail.value
    })
  },



  /**
   * 点击页面隐藏键盘事件
   */
  hideKeyboard: function () {
    var self = this;
    if (self.data.isKeyboard) {
      //说明键盘是显示的，再次点击要隐藏键盘
      if (self.data.textValue) {
        self.setData({
          isKeyboard: false
        })
      } else {
        self.setData({
          isKeyboard: false,
          isFocus: false
        })
      }

    }
  },
  /**
   * 输入框聚焦触发，显示键盘
   */
  bindFocus: function () {
    var self = this;
    if (self.data.isKeyboard) {
      //说明键盘是显示的，再次点击要隐藏键盘
      self.setData({
        isKeyboard: false,
        isFocus: true,
      })
    } else {
      //说明键盘是隐藏的，再次点击显示键盘
      self.setData({
        isFocus: true,
        isKeyboard: true,
      })
    }
  },
  /**
   * 键盘事件
   */
  tapKeyboard: function (e) {
    var self = this;
    //获取键盘点击的内容，并将内容赋值到textarea框中
    var tapIndex = e.target.dataset.index;
    var tapVal = e.target.dataset.val;
    var keyboardValue;
    var specialBtn;
    var tapNum;
    if (tapVal == "巛") {
      //说明是删除 
      self.data.textArr.pop();
      if (self.data.textArr.length == 0) {
        //说明没有数据了，返回到省份选择键盘
        this.specialBtn = false;
        this.tapNum = false;
        this.keyboardValue = self.data.keyboard1;
      } else if (self.data.textArr.length == 1) {
        //只能输入字母
        this.tapNum = false;
        this.specialBtn = true;
        this.keyboardValue = self.data.keyboard2;
      } else {
        this.specialBtn = true;
        this.tapNum = true;
        this.keyboardValue = self.data.keyboard2;
      }
      self.data.textValue = self.data.textArr.join("");
      self.setData({
        textValue: self.data.textValue,
        keyboardValue: this.keyboardValue,
        specialBtn: this.specialBtn,
        tapNum: this.tapNum,
      })
      return false
    }
    if (self.data.textArr.length >= 8) {
      return false;
    }
    self.data.textArr.push(tapVal);
    self.data.textValue = self.data.textArr.join("");
    self.setData({
      textValue: self.data.textValue,
      keyboardValue: self.data.keyboard2,
      specialBtn: true,
    })
    if (self.data.textArr.length > 1) {
      //展示数字键盘
      self.setData({
        tapNum: true
      })
    }
  },
  /**
   *绑定挪车码
   */
  tapBindBtn: function (e) { 
    var self = this;
    let auto_no = self.data.textValue;
    let phone = self.data.inputPhoneValue;
    let verCode = self.data.verCode;
    let name = self.data.inputNameValue; 

    if (self.data.flag) {
      return false
    } else {
      //说明是完成事件
      if (auto_no.length < 7) {
        errorMsg('车牌号不正确');
      } else if (name == "") {
        errorMsg('姓名不能为空');
      } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone)) {
        errorMsg('手机号不正确');
      } else if (verCode == "") {
        errorMsg('验证码不能为空');
      }
      else {
        //数据验证完毕发送接口请求 
        self.setData({
          flag: true
        })
        qcodeBind(auto_no, name, phone, self.data.qcode, verCode, (r) => {
          if (r.code == 0) {
            //绑定成功
            this.toggleBaseDialog();
          } else {
            //绑定失败
            errorMsg(r.message);
          }
          self.setData({
            flag: false
          })
        })
      }
    }

  },
  sendCode: function () {
    //发送验证码
    let self = this;
    let phone = this.data.inputPhoneValue;
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone)) {
      errorMsg('手机号不正确');
    } else {
      smsSend(phone, (r) => {
        debugger;
        if (r.code == 0) {
          this.setData({
            downtime: --self.data.downtime
          })
          let siv = setInterval(() => {
            if (self.data.downtime > 1) {
              self.setData({
                downtime: --self.data.downtime
              })
            } else {
              clearInterval(siv);
              self.setData({
                downtime: 60
              })
            }

          }, 1000)
        } else {
          errorMsg(r.message);
        }
      })
    }
  },
  //绑定成功提示
  toggleBaseDialog() {
    Dialog({
      title: '温馨提示',
      message: '绑定成功\n请将二维码贴于车辆挡风玻璃上。',
      selector: '#zan-base-dialog',
    }).then(() => {
      //跳转到首页
      wx.redirectTo({
        url: '../newusercenter/newusercenter?qcode=' + this.data.qcode + "&auto_number=" + this.data.textValue
      })
    });
  },
  /**
   * 授权获取手机号
   */
  getPhoneNumber: function (e) {
    getDecryptInfo(e.detail.iv, e.detail.encryptedData,(r)=>{ 
      this.setData({
        inputPhoneValue:r.data
      })  
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var self = this;
    //将keyboard1和keyboard2中的所有字符串拆分成一个一个字组成的数组
    self.data.keyboard1 = self.data.keyboard1.split('')
    self.data.keyboard2 = self.data.keyboard2.split('')
    self.setData({
      keyboardValue: self.data.keyboard1
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var self = this;
    self.setData({
      flag: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.stopPullDownRefresh()
  }

})